package pi.client.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import pi.client.board.Board;
import pi.client.core.MyProgram;
import pi.client.network.ClientNetwork;
import pi.client.players.Player;
import pi.client.utils.ImageUtils;

@SuppressWarnings("serial")
public class MyMainPanel extends JPanel implements MouseWheelListener, MouseMotionListener, MouseListener, KeyListener {
	private MyPanelBase activePanel;
	protected MyPanelIntro introPanel;
	protected MyPanelLogin loginPanel;
	protected MyPanelPerfil perfilPanel;
	protected MyPanelOptions optionsPanel;
	public MyPanelPreGame preGamePanel;
	public MyPanelGame gamePanel;
	public MyPanelEndGame endGamePanel;
	
	public MyProgram program;
	
	public Board board;

	public Player playerMe;
	public Player playerEnemy;

	public ClientNetwork connection;
	
	public BufferedImage spriteUnitKing;
	public BufferedImage spriteUnitSoldier;
	public BufferedImage spriteUnitSoldierArcher;
	
	private BufferedImage boardBackground;
	
//	public BufferedImage tileGrass = ImageUtils.LoadImage("res/images/game/board/tiles/tileGrass.png");
//	public BufferedImage tileGround = ImageUtils.LoadImage("res/images/game/board/tiles/tileGround.png");
//	public BufferedImage tileMountain = ImageUtils.LoadImage("res/images/game/board/tiles/tileMountain.png");
//	public BufferedImage tileRiver = ImageUtils.LoadImage("res/images/game/board/tiles/tileRiver.png");
	
	public MyMainPanel(MyProgram program){
		this.program = program;
		
		addKeyListener(this);
		addMouseListener(this);
		addMouseMotionListener(this);
		addMouseWheelListener(this);

		setBackground(Color.red);
		setLayout(null);
		setLocation(0, 0);
		setPreferredSize(new Dimension(MyProgram.ScreenWidth, MyProgram.ScreenHeight));

		spriteUnitKing = ImageUtils.LoadImage("res/images/units/logos/Crown.png");
		spriteUnitSoldier = ImageUtils.LoadImage("res/images/units/logos/Sword.png");
		spriteUnitSoldierArcher = ImageUtils.LoadImage("res/images/units/logos/Bow.png");
		
		boardBackground = ImageUtils.LoadImage("res/images/game/board/background.png");
		
//		tileGrass = ImageUtils.LoadImage("res/images/game/board/tiles/tileGrass.png");
//		tileGround = ImageUtils.LoadImage("res/images/game/board/tiles/tileGround.png");
//		tileMountain = ImageUtils.LoadImage("res/images/game/board/tiles/tileMountain.png");
//		tileRiver = ImageUtils.LoadImage("res/images/game/board/tiles/tileRiver.png");
		
		board = new Board(program, boardBackground); 
		
		playerMe = new Player(program);
		playerEnemy = new Player(program);
		
		connection = new ClientNetwork(program);

		introPanel = new MyPanelIntro(this, "res/images/panels/panelIntro.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(introPanel);

		loginPanel = new MyPanelLogin(this, "res/images/panels/panelLogin.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(loginPanel);
		
		perfilPanel = new MyPanelPerfil(this, "res/images/panels/panelPerfil.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(perfilPanel);
		
		optionsPanel = new MyPanelOptions(this, "res/images/panels/panelOptions.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(optionsPanel);
		
		preGamePanel = new MyPanelPreGame(this, "res/images/panels/panelPreGame.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(preGamePanel);
		
		gamePanel = new MyPanelGame(this, "res/images/panels/panelPreGame.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(gamePanel);
		
		endGamePanel = new MyPanelEndGame(this, "res/images/panels/panelEndGame.png", MyProgram.ScreenWidth, MyProgram.ScreenHeight);
		add(endGamePanel);

		SetCurrentPanel(introPanel);
	}

	public void SetCurrentPanel(MyPanelBase panel){
		int numberOfPanels = getComponentCount();
		for(int i = 0; i < numberOfPanels; i++){
			getComponent(i).setVisible(false);
		}

		activePanel = panel;
		activePanel.setBounds(0, 0, panel.width, panel.height);
		activePanel.setVisible(true);
	}
	
	public void ResetPlayers(){
		String myName = playerMe.username;
		playerMe = null;
		playerEnemy = null;
		
		playerMe = new Player(program);
		playerMe.username = myName;
		playerEnemy = new Player(program);
	}

	// key listener
	@Override public void keyPressed(KeyEvent e){		
		activePanel.KeyPressedEvent(e);
	}
	@Override public void keyReleased(KeyEvent e){
		activePanel.KeyReleasedEvent(e);
	}
	@Override public void keyTyped(KeyEvent e){
		activePanel.KeyTypedEvent(e);
	}

	// mouse listener
	@Override public void mouseClicked(MouseEvent e){
		activePanel.MouseClickedEvent(e);
	}
	@Override public void mouseEntered(MouseEvent e){
		activePanel.MouseEnteredEvent(e);
	}
	@Override public void mouseExited(MouseEvent e){
		activePanel.MouseExitedEvent(e);
	}
	@Override public void mousePressed(MouseEvent e){
		activePanel.MousePressedEvent(e);
	}
	@Override public void mouseReleased(MouseEvent e){
		activePanel.MouseReleasedEvent(e);
	}

	// mouse motion listener
	@Override public void mouseDragged(MouseEvent e){
		activePanel.MouseDraggedEvent(e);
	}
	@Override public void mouseMoved(MouseEvent e){
		activePanel.MouseMovedEvent(e);
	}

	// mouse wheel listener
	@Override public void mouseWheelMoved(MouseWheelEvent e){
		activePanel.MouseWheelMovedEvent(e);
	}
}